package com.orange.adminapi.controller.product;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.product.CreditProduct;
import com.orange.core.pojo.req.product.CreditProductPageReq;
import com.orange.core.pojo.req.product.CreditProductStockUpdateReq;
import com.orange.core.service.product.CreditProductService;
import com.orange.core.validator.group.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 积分商品
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/creditProduct")
public class CreditProductController {

    private final CreditProductService creditProductService;

    /**
     * 列表
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<CreditProduct>> page(@RequestBody PageReqDto<CreditProductPageReq> req) {
        return R.ok(creditProductService.page(req));
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public R<CreditProduct> info(@PathVariable int id) {
        return R.ok(creditProductService.info(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated CreditProduct creditProduct) {
        return R.ok(creditProductService.save(creditProduct.withId(null)));
    }

    /**
     * 编辑
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated(Update.class) CreditProduct creditProduct) {
        return R.ok(creditProductService.updateById(creditProduct));
    }

    /**
     * 上下架
     *
     * @param shelf 上架状态：1上架，0下架
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_SHELF.permission.identifier)")
    @PostMapping("/shelf/{id}/{shelf}")
    public R<Boolean> shelf(@PathVariable int id, @PathVariable("shelf") int shelf) {
        return R.ok(creditProductService.shelf(id, shelf));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_STOCK.permission.identifier)")
    @PostMapping("/stock/{id}")
    public R<Boolean> stock(@PathVariable int id, @RequestBody CreditProductStockUpdateReq req) {
        return R.ok(creditProductService.changeStock(id, req.getStock()));
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).CREDIT_PRODUCT_DELETE.permission.identifier)")
    @DeleteMapping("/{id}")
    public R<Boolean> del(@PathVariable int id) {
        return R.ok(creditProductService.removeById(id));
    }
}
